package de.szut.dqi12.sqlitebrowser.controller;

import java.awt.Rectangle;
import java.io.File;

import de.szut.dqi12.sqlitebrowser.model.PropertiesAnalyzer;
import de.szut.dqi12.sqlitebrowser.model.SQLite;
import de.szut.dqi12.sqlitebrowser.model.Table;
import de.szut.dqi12.sqlitebrowser.view.View;

/**
 * Controller
 * Controlls communication between Model and View
 * @author Alexander
 */

public class Controller {

	/**
	 * Static instance of this Class (Singleton)
	 */
	private static Controller instance;
	
	private SQLite connection;
	private PropertiesAnalyzer propertieAnalyzer;
	private View gui;
	
	/**
	 * Private constructor -> Singleton Pattern
	 */
	private Controller() {}
	
	/**
	 * Method to initialize and start the program
	 */
	public void init() {
		connection = new SQLite();
		propertieAnalyzer = new PropertiesAnalyzer();
		gui = new View();
	}
	
	/**
	 * Singleton Patter getInstance Method
	 * @return Instance of the Controller class
	 */
	
	public static Controller getInstance() {
		if(instance == null) {
			instance = new Controller();
		}
		return instance;
	}
	
	/**
	 * Calls a Method in the Model to execute a specific sql query
	 * @param query - raw sql query
	 * @param isLimited - show if there is a limit of rows to be displayed
	 * @param start - first row
	 * @param end - last row
	 * @return Table object with all informations to update the JTable on the view
	 */
	public Table executeQuery(String query, boolean isLimited, String start, String end) {
		Table content = null;
		if(isLimited) {
			try {
				content = connection.executeQuery(query, Integer.valueOf(start) - 1, Integer.valueOf(end));
			} catch (Exception e) {
				executeErrorMessageDialog(ErrorMessages.SQL_QUERY_FAILED);
				content = connection.executeQuery(query);
			}
		} else {
			content = connection.executeQuery(query);
		}
		return content;
	}

	/**
	 * calls the executeQuery Method to execute the SQL Query to open a table
	 * @param tableName - Name of the table
	 * @param isLimited - show if there is a limit of rows to be displayed
	 * @param start - first row
	 * @param end - last row
	 * @return Table object with all informations to update the JTable on the view
	 */
	
	public Table openTable(String tableName, boolean isLimited, String start, String end) {
		return executeQuery("Select * From " + tableName, isLimited, start, end);
	}

	/**
	 * Creates a connection to a .db3 database File and update the view with the database informations
	 * @param selectedFile - .db3 database file
	 */
	
	public void openDatabase(File selectedFile) {
		if(selectedFile != null) {
			if(gui.addDatabaseToTree(selectedFile.getName().substring(0, selectedFile.getName().lastIndexOf(".")), connection.openDatabase(selectedFile))) {
				setLastDatabasePath(selectedFile.getAbsolutePath());
				gui.setDatabaseConnection(true);
			} else {
				executeErrorMessageDialog(ErrorMessages.NO_DATABASE_CHOOSED);
			}
		} else {
			executeErrorMessageDialog(ErrorMessages.NO_DATABASE_CHOOSED);
		}
	}
	
	/**
	 * Gets the frame size and position from the propertieAnalyzer class
	 * @return A rectangle with position and size data
	 */
	
	public Rectangle getFrameBounds() {
		return propertieAnalyzer.getFrameBounds();
	}
	
	/**
	 * Sets the frame size and position with the propertieAnalyzer class
	 * @param frameBounds - A rectangle with position and size data of the Frame
	 */
	
	public void setFrameBounds(Rectangle frameBounds) {
		propertieAnalyzer.setFrameBounds(frameBounds);
	}
	
	/**
	 * Gets the path of the last displayed database
	 * @return The path as a String
	 */
	
	public String getLastDatabasePath() {
		return propertieAnalyzer.getLastDatabaseConnectionPath();
	}
	
	/**
	 * Sets the path of the last displayed database
	 * @param The path as a String
	 */
	
	public void setLastDatabasePath(String path) {
		propertieAnalyzer.setLastDatabaseConnection(path);
	}
	
	/**
	 * Returns the instance of the View class
	 * @return instance of the View class
	 */
	public View getView() {
		return gui;
	}
	
	/**
	 * Calls the executeErrorMessageDialog method of the View to display an Error
	 * @param errorMessage - Errormessage
	 */
	
	public void executeErrorMessageDialog(String errorMessage) {
		gui.executeErrorMessageDialog(errorMessage);
	}

}